package ioc

import (
	"github.com/IBM/sarama"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"jk-time/webook/interactive/dao"
	"jk-time/webook/pkg/ginx"
	"jk-time/webook/pkg/gormx/connpool"
	"jk-time/webook/pkg/logger"
	"jk-time/webook/pkg/migrator/events"
	"jk-time/webook/pkg/migrator/events/fixer"
	"jk-time/webook/pkg/migrator/scheduler"
)

var topic = "inconsistent_interactive"

func InitMigratorServer(l logger.Logger, src SrcDB, dst DstDB,
	pool *connpool.DoubleWritePool, producer events.Producer) *ginx.Server {
	// 在这里，有多少张表，你就初始化多少个scheduler
	intrSch := scheduler.NewScheduler[dao.Interactive](l, src, dst, pool, producer, connpool.PatternSrcOnly)
	engine := gin.Default()
	intrSch.RegisterRoutes(engine.Group("/migrator"))
	addr := viper.GetString("migrator.addr")

	return &ginx.Server{
		Engine: engine,
		Addr:   addr,
	}
}
func InitInteractiveProducer(p sarama.SyncProducer) events.Producer {
	return events.NewSaramaProducer(p, topic)
}
func InitInteractiveConsumer(client sarama.Client, l logger.Logger, src SrcDB, dst DstDB) *fixer.Consumer[dao.Interactive] {
	res, err := fixer.NewConsumer[dao.Interactive](client, l, topic, src, dst)
	if err != nil {
		panic(err)
	}
	return res
}
